/**   
*    
* 项目名称：SmartKungFuService   
* 类名称：OpenImUserServiceImpl   
* 类描述：   
* 创建人：Wdd   
* 创建时间：2016年12月9日 上午9:54:25   
* 修改人：Wdd  
* 修改时间：2016年12月9日 上午9:54:25   
* 修改备注：   
* @version    
*    
*/
package com.lst.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.lst.common.CommCode;
import com.lst.dao.MstClientMapper;
import com.lst.model.MstClient;
import com.lst.service.OpenImUserService;
import com.lst.utils.CommonUtils;
import com.lst.utils.DateUtil;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.domain.Userinfos;
import com.taobao.api.request.OpenimUsersAddRequest;
import com.taobao.api.request.OpenimUsersUpdateRequest;
import com.taobao.api.response.OpenimUsersAddResponse;
import com.taobao.api.response.OpenimUsersUpdateResponse;

/**
 * @ClassName: OpenImUserServiceImpl
 * @Description: OpenIm用户
 * @author Wdd
 * @date 2016年12月9日 上午9:54:25
 *
 */
@Service("openImUserServiceImpl")
public class OpenImUserServiceImpl implements OpenImUserService {
	
	private Logger logger = Logger.getLogger("log");
	
	@Autowired
	private MstClientMapper mstClientMapper;

	/**
	 *  OpenIm用户初始化
	 */
	@Override
	public MstClient OpenImUserInit(MstClient mstClient) {
		
		Date startDate = new Date();
		logger.info("OpenImUserInit begin:" + startDate);
		
		//查询APP用户
		MstClient appClient = mstClientMapper.selectByPrimaryKey(mstClient.getId());
		
		//打开通道
		TaobaoClient client = new DefaultTaobaoClient("http://gw.api.taobao.com/router/rest", "23560261", "54cd3ccadbc71825dc049ed49751f087");
		
		if(!StringUtils.hasText(appClient.getReserved9())){
			//没有OpenIM用户，需注册
			try {
				//新增OpenIM用户
				List<Userinfos> list = new ArrayList<>();
				
				Userinfos i = new Userinfos();
				//用户名
				i.setUserid("smartkungfu" + mstClient.getId());
				//密码
				i.setPassword("smartkungfu" + mstClient.getId());
				
				/**
				 * 头像
				 */
				String portrait = "";
				
				if(!StringUtils.hasText(appClient.getPortrait())){
					//如果头像为空，取默认头像
					portrait = CommonUtils.getSite_name() + CommCode.OPEN_IM_USER_ICON;
				}else{
					if(appClient.getPortrait().indexOf(CommCode.UPLOAD_IMAGES) != -1){
						//自己平台的头像链接
						portrait = CommonUtils.getSite_name() + appClient.getPortrait();
					}else{
						//第三方头像链接
						portrait = appClient.getPortrait();
					}
					
				}
				
				i.setIconUrl(portrait);
				
				list.add(i);
				
				OpenimUsersAddRequest req = new OpenimUsersAddRequest();
				req.setUserinfos(list);
				
				OpenimUsersAddResponse response = client.execute(req);
				
				//创建OpenIM用户成功
				if(response.isSuccess()){
					//更新本地app用户表
					mstClient.setReserved9("smartkungfu" + mstClient.getId());
					mstClient.setReserved10("smartkungfu" + mstClient.getId());
					mstClient.setReserved11(portrait);
					mstClientMapper.updateByPrimaryKeySelective(mstClient);
				}
				
				System.out.println(response.getBody());
				logger.info(response.getBody());
			} catch (ApiException e) {
				
				logger.error("OpenImUserInit error: ", e);
			}
		}else{
			//已经存在，更新头像
			try {
				OpenimUsersUpdateRequest req = new OpenimUsersUpdateRequest();
				
				List<Userinfos> list = new ArrayList<>();
				
				Userinfos i = new Userinfos();
				//用户名
				i.setUserid(mstClient.getReserved9());
				
				/**
				 * 头像
				 */
				String portrait = "";
				
				if(!StringUtils.hasText(appClient.getPortrait())){
					//如果头像为空，取默认头像
					portrait = CommonUtils.getSite_name() + CommCode.OPEN_IM_USER_ICON;
				}else{
					if(appClient.getPortrait().indexOf(CommCode.UPLOAD_IMAGES) != -1){
						//自己平台的头像链接
						portrait = CommonUtils.getSite_name() + appClient.getPortrait();
					}else{
						//第三方头像链接
						portrait = appClient.getPortrait();
					}
				}
				
				i.setIconUrl(portrait);
				
				list.add(i);
				
				req.setUserinfos(list);
				
				OpenimUsersUpdateResponse rsp = client.execute(req);
				
				//更新OpenIM用户成功
				if(rsp.isSuccess()){
					//更新本地app用户表
					mstClient.setReserved11(portrait);
					mstClientMapper.updateByPrimaryKeySelective(mstClient);
				}
				
				System.out.println(rsp.getBody());
				logger.info(rsp.getBody());
			} catch (Exception e) {
				logger.error("OpenImUserUpadate error: ", e);
			}
		}
		
		logger.info("OpenImUserInit end: " + DateUtil.calLastedTime(startDate));
		
		return mstClient;
	}

}
